package cn.iocoder.yudao.module.magic.api;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.magic.api.dto.ApiInfoDTO;
import cn.iocoder.yudao.module.magic.service.MagicApiResourcService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import org.ssssssss.magicapi.core.model.ApiInfo;
import org.ssssssss.magicapi.core.model.Group;
import org.ssssssss.magicapi.core.model.TreeNode;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * Magic Api 实现类
 *
 */
@RestController
@Validated
public class MagicApiListImpl implements MagicApiList {


    private static final String pathRoot = "/";


    @Resource
    private MagicApiResourcService magicApiResourcService;



    @Override
    public CommonResult<ArrayList> getApiList() {

        TreeNode<Group> apiTree = magicApiResourcService.getApiTree();

        ArrayList<ApiInfoDTO> res = new ArrayList<>();
        coverGroup2ApiInfoDTO(apiTree,res,"","");
        return success(res);
    }

    private void coverGroup2ApiInfoDTO(TreeNode<Group> apiTree,ArrayList<ApiInfoDTO> res,String parentPath,String parentDir){
        if(apiTree.getNode() != null){
            coverGroup2ApiInfoDTO(apiTree.getNode(),res,parentPath,parentDir);
        }
        if(apiTree.getChildren() != null){
            coverGroup2ApiInfoDTO(apiTree.getChildren(),res,parentPath,parentDir);
        }
    }

    private void coverGroup2ApiInfoDTO(List<TreeNode<Group>> children,ArrayList<ApiInfoDTO> res,String parentPath,String parentDir){

        children.stream().forEach(group -> coverGroup2ApiInfoDTO(group,res, parentPath,parentDir));
    }

    private void coverGroup2ApiInfoDTO(Group node,ArrayList<ApiInfoDTO> res,String parentPath,String parentDir){
        if("0".equals(node.getId())){
            return;
        }
        // 最底层文件夹
        ApiInfoDTO apiInfoDTO = new ApiInfoDTO()
                .setId(node.getId())
                .setName(node.getName())
                .setType(node.getType())
                .setParentId(node.getParentId())
                .setPath(node.getPath())
                .setFullPath(appendPath(parentPath,node.getPath()))
                .setFullDir(appendPath(parentDir,node.getName()))
                .setLeaf(false);
//        res.add(
//                apiInfoDTO
//        );
        parentPath = apiInfoDTO.getFullPath();
        parentDir = apiInfoDTO.getFullDir();

        // 获取api接口列表
        List<ApiInfo> magicEntities = magicApiResourcService.listFiles(apiInfoDTO.getId());
        coverMagicEntity2ApiInfoDTO(magicEntities,res,parentPath,parentDir);
    }


    private void coverMagicEntity2ApiInfoDTO(List<ApiInfo> apiInfoList,ArrayList<ApiInfoDTO> res,String parentPath,String parentDir){
        apiInfoList.stream().forEach(apiInfo -> coverMagicEntity2ApiInfoDTO(apiInfo,res,parentPath,parentDir));
    }
    private void coverMagicEntity2ApiInfoDTO(ApiInfo apiInfo,ArrayList<ApiInfoDTO> res,String parentPath,String parentDir){
        // 最底层文件夹
        ApiInfoDTO apiInfoDTO = new ApiInfoDTO()
                .setId(apiInfo.getId())
                .setName(apiInfo.getName())
                .setType("apiInfo")
                .setParentId(apiInfo.getGroupId())
                .setPath(apiInfo.getPath())
                .setMethod(apiInfo.getMethod())
                .setFullPath(appendPath(parentPath,apiInfo.getPath()))
                .setFullDir(appendPath(parentDir,apiInfo.getName()))
                .setLeaf(true);
        res.add(
                apiInfoDTO
        );
    }


    private String appendPath(String parentPath,String path){
        return new StringBuilder(parentPath).append(path.startsWith(pathRoot)?path : pathRoot+path).toString();
    }
}
